/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package rentcalculator.model;

import java.util.Objects;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import static javax.persistence.GenerationType.IDENTITY;
import javax.persistence.Id;

/**
 *
 * @author K.E.Y
 */

@Entity
public class Discount  {
    
    @Id
    @GeneratedValue(strategy = IDENTITY)
    private long id;
    
    //if this discount can exists with others
    private boolean exclusive;
    
    //only support amount and percentage
    @Column(name = "unit_of_measure", nullable = false)
    private String unitOfMeasure;
    
    private double amount;
    
    private String name;
    
    private boolean active;
    
    private int priority;

    public int getPriority() {
        return priority;
    }

    public void setPriority(int priority) {
        this.priority = priority;
    }
    
    

    public boolean isActive() {
        return active;
    }

    public void setActive(boolean active) {
        this.active = active;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
    
    public Discount(){
        this.exclusive = true;
        this.active = false;
    }

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public boolean isExclusive() {
        return exclusive;
    }

    public void setExclusive(boolean exclusive) {
        this.exclusive = exclusive;
    }

    public String getUnitOfMeasure() {
        return unitOfMeasure;
    }

    public void setUnitOfMeasure(String unitOfMeasure) {
        this.unitOfMeasure = unitOfMeasure;
    }

    public double getAmount() {
        return amount;
    }

    public void setAmount(double amount) {
        this.amount = amount;
    }

    @Override
    public int hashCode() {
        int hash = 7;
        hash = 67 * hash + (int) (this.id ^ (this.id >>> 32));
        hash = 67 * hash + (this.exclusive ? 1 : 0);
        hash = 67 * hash + Objects.hashCode(this.unitOfMeasure);
        hash = 67 * hash + (int) (Double.doubleToLongBits(this.amount) ^ (Double.doubleToLongBits(this.amount) >>> 32));
        hash = 67 * hash + Objects.hashCode(this.name);
        hash = 67 * hash + (this.active ? 1 : 0);
        hash = 67 * hash + this.priority;
        return hash;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final Discount other = (Discount) obj;
        if (this.id != other.id) {
            return false;
        }
        if (this.exclusive != other.exclusive) {
            return false;
        }
        if (!Objects.equals(this.unitOfMeasure, other.unitOfMeasure)) {
            return false;
        }
        if (Double.doubleToLongBits(this.amount) != Double.doubleToLongBits(other.amount)) {
            return false;
        }
        if (!Objects.equals(this.name, other.name)) {
            return false;
        }
        if (this.active != other.active) {
            return false;
        }
        return this.priority == other.priority;
    }

   
    
        public double discount(double amount){
            if("AMOUNT".equals(this.unitOfMeasure)){
                return amount - this.amount;
            }
            if("PERCENT".equals(this.unitOfMeasure)){
                return this.amount/100* amount;
            }
            System.out.println("Unit of measure is not supported: " + this.unitOfMeasure);
            return amount;
        }
    
}
